import { getMenuList } from "@/api/common/index.ts"; 
import { defineStore } from "pinia"; 
/**
 * 将路由处理为菜单的树状结构
 * @param list 
 * @param parentId 
 * @returns 
 */
export function dealWithRouterToMenu(list: Array<menuType>, parentId = 0) {
  let result: Array<menuType> = [];
  list.forEach((item) => {
    // 排除隐藏的菜单
    if (item.parentId === parentId && item.hidden !== 1) {
      result.push({ ...item, children: dealWithRouterToMenu(list, item.id) });
    }
  });
  return result;
}



export const useRoutesStore = defineStore("routers", {
  state: (): { routes: Array<menuType> } => ({
    routes: [],
  }),
  actions: {
    async getRoutesList() { 
      const data = await getMenuList() || []; 
      const routes = data?.list || [];
      this.routes = routes;
      return routes;
    },
  },
  getters: {
    menus: (state) => {
      return dealWithRouterToMenu(state.routes); 
    },
  },
});
